﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

using AppDialogBox;

namespace PasswordControl
{
    public partial class SaveProfile_Form : Form
    {
        public enum SaveProfileMode
        {
            SaveAndContinue,    //save and return to main window
            SaveAndLoad,        //save and load a new profile
            SaveAndExit,        //save and exit the program
            SaveAndNew,         //save and start new profile
            NotSaved
        }

        private SaveProfileMode m_saveMode = SaveProfileMode.SaveAndContinue;
        public SaveProfileMode SaveMode
        {
            get { return m_saveMode; }
            set { m_saveMode = value; }
        }
        //if profile was saved than confirm=true , else if canceled than false.
        public bool ProfileSaved { get; set; }

        private ControlProgram m_controlform;

        public SaveProfile_Form(ControlProgram control_form)
        {
            InitializeComponent();

            this.m_controlform = control_form;
        }

        public SaveProfile_Form(ControlProgram control_form, string password)
        {
            InitializeComponent();

            this.m_controlform = control_form;

            this.TextPassword.Text = password;
        }

        private void SaveProfile_Form_Load(object sender, EventArgs e)
        {
            //add profiles to profile box
            string sourceDir = ControlProgram.PROFILES_PATH;
            string[] fileEntries = Directory.GetFiles(sourceDir);
            foreach (string fileName in fileEntries)
            {
                this.ProfileBox.Items.Add(Path.GetFileNameWithoutExtension(fileName));
            }
        }

        private void ProfileOK_Click(object sender, EventArgs e)
        {
            if (this.ProfileText.Text.Length == 0)
            {
                AppDialogWrapper.PromptBox("Profile name must be given","Input Error");
                return;
            }
            if (this.ProfileText.Text.CompareTo(ControlProgram.DEFAULT_PROFILE_NAME) == 0)
            {
                AppDialogWrapper.PromptBox("Can't name a profile: new profile","Input Error");
                return;
            }
            if (this.TextPassword.Text.CompareTo(this.TextConfirmPassword.Text) != 0)
            {
                AppDialogWrapper.PromptBox("Passwords don't match", "Input Error");
                return;
            }
            if (this.ProfileBox.Items.Contains(this.ProfileText.Text) && AppDialogWrapper.PromptBox("Profile exists, overwrite?", "Confirm", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;

            this.Hide();
            this.SaveProfile(this.ProfileText.Text);          
        }

        private void ProfileCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.ProfileSaved = false;

            this.FinishSaveProfile();
        }

        private void ProfileBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ProfileText.Text = this.ProfileBox.Text;
        }

        public void SaveProfile(string profilename)
        {
            this.ProfileSaved = true;

            string path = ControlProgram.GetProfilePath(profilename, true);
            List<Owners> data = Owners.GetOwners();

            if (DataSerialize.SaveEncrypted<List<Owners>>(data, path, this.TextPassword.Text))
            {
                m_controlform.CurrentPassword = this.TextPassword.Text;
            }

            //save profile defaults
            ProfileData profileData = ProfileData.FindProfileData(profilename);
            if (profileData == null) profileData = ProfileData.ProfileDataAdd(profilename);
            profileData.SearchContain = m_controlform.CheckBoxContain.Checked;
            profileData.DisplayData = m_controlform.CheckBoxDisplay.Checked;
            ProfileData.SaveProfileData();
            m_controlform.DefaultChanged = false;

            this.FinishSaveProfile();
        }

        private void FinishSaveProfile()
        {
            this.m_controlform.Activate();

            if (this.ProfileSaved)
            {
                this.m_controlform.ListChanged = false;
                this.m_controlform.labelSaved.Visible = true;
                if (this.ProfileText.Text.Length > 0) this.m_controlform.ProfileLabel = this.ProfileText.Text;
            }
            else return;

            switch (SaveMode)
            {
                case SaveProfileMode.SaveAndContinue: 
                    break;
                case SaveProfileMode.SaveAndLoad: 
                    LoadProfile_Form loadform = new LoadProfile_Form(this.m_controlform);
                    loadform.ShowDialog();
                    break;
                case SaveProfileMode.SaveAndExit: 
                    m_controlform.Hide();
                    m_controlform.Dispose();
                    break;
                case SaveProfileMode.SaveAndNew: 
                    m_controlform.MenuNew_Click(new object(), new EventArgs());
                    break;
            }
        }
    }
}
